import { sliceUpload } from "@/common/fileUtils/sliceUpload";
import { get, post } from "@/common/useAxios";
import { TUseIndicatorsDictionary } from "@/gcfx_web/defaultConfig/config";
import { openMessage } from "@/hy_dev_lib/src";
import { Button, Space, Upload, UploadProps } from "antd";
import { useEffect, useState } from "react";

// 指标字典
const useIndicatorsDictionary: TUseIndicatorsDictionary = () => {
  return {
    hideSearch: true, // 是否隐藏搜索框
    OperateBox, // 右上角操作按钮 私有云有导入和更新
  };
};

export default useIndicatorsDictionary;

const OperateBox = () => {
  const [isUpdateState, setIsUpdateState] = useState(false);

  useEffect(() => {
    isUpdate();
  }, []);

  //判断是否更新
  const isUpdate = async () => {
    const res = await get(
      "1500170",
      null,
      {
        showError: false,
      }
    );
    setIsUpdateState(true);
  };

  //点击更新字典按钮的回调
  const updataDictionary = () => {
    get("1500171", null);
    openMessage({
      type: "success",
      content: "字典更新成功！",
    });
  };

  //上传模板
  const propsup: UploadProps = {
    name: "file",
    async onChange(info) {
      const { file } = info;

      // const url = (await sliceUpload(file, { objType: "TemplateManagement", send: 0 })) as string;
      const uploadRes = await sliceUpload(file, "TemplateManagement");
      const url = uploadRes?.data?.[0]?.urls;
      post("1500176", { path: url }).then((res) => {
        if (res.code == 200) {
          openMessage({
            type: "success",
            content: "导入成功!",
          });
        }
      });
      if (info.file.status === "done") {
        // console.log("info", info);
      } else if (info.file.status === "error") {
        openMessage({
          type: "error",
          content: `${info.file.name}文件导入失败`,
        });
      }
    },
    showUploadList: false, //不显示文件列表
    beforeUpload: (file) => {
      return false;
    },
  };

  return (
    <Space>
      <Upload {...propsup} accept=".txt" maxCount={1}>
        <Button type="primary">导入数据</Button>
      </Upload>
      <Button type="primary" disabled={isUpdateState ? false : true} onClick={updataDictionary}>
        更新字典
      </Button>
    </Space>
  );
};
